from django.db import models
from apps.utils.db import table_prefix, CoreModel


class Permission(CoreModel):
    """
    功能权限:菜单,按钮
    """

    PERM_TYPE_MENU = 10
    PERM_TYPE_BUTTON = 20
    menu_type_choices = (
        (PERM_TYPE_MENU, '菜单'),
        (PERM_TYPE_BUTTON, '按钮')
    )
    name = models.CharField('名称', max_length=30)
    parent = models.ForeignKey('self', verbose_name='父级', on_delete=models.CASCADE, null=True, blank=True, db_constraint=False)
    type = models.PositiveSmallIntegerField('类型', choices=menu_type_choices, default=20)
    sort = models.PositiveSmallIntegerField('排序标记', default=1)
    level = models.PositiveSmallIntegerField('层级', default=1)
    codes = models.CharField(verbose_name='前端路由标识', default='', max_length=255, null=True, blank=True)
    urls = models.CharField(verbose_name='路径', help_text="路径", default='', max_length=255, null=True, blank=True)
    method_types = models.CharField(verbose_name='请求方式', help_text="请求方式", default='GET', max_length=20, null=True, blank=True)
    is_active = models.BooleanField(verbose_name='是否禁用', default=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = table_prefix + 'custom_permissions'
        verbose_name = '功能权限表'
        verbose_name_plural = verbose_name
        ordering = ['id']
